$(function () {

    // var arr = ["耐克", "李宁", "新百伦", "耐克王", "阿迪王"];
    // localStorage.setItem('search-list', JSON.stringify(arr))
    function render() {
        var arr = JSON.parse(localStorage.getItem('search-list')) || []
        var obj = {
            list: arr
        }
        $(".lt-search-history").html(template("tmp", obj))


    }
    render()
    //清空历史
    $(".lt-search-history").on("click", ".btn-clear", function () {
        mui.confirm("是否清空历史", "警告", ["取消", "确定"], function (e) {
            console.log(e);
            if (e.index == 1) {
                localStorage.removeItem("search-list")
            }
            render()
        })
    })
    //删除一条历史
    $(".lt-search-history").on("click", ".btn-del", function () {
        var index = $(this).data('index')
        console.log(index);
        mui.confirm("是否删除这条历史", "警告", ["取消", "确定"], function (e) {
            var arr = JSON.parse(localStorage.getItem("search-list"))
            if(e.index==1) {
                arr.splice(index,1)
                console.log(arr);
                localStorage.setItem("search-list", JSON.stringify(arr))
                render()
            }
          
        })
    })
    //点击搜索按钮，添加一条搜索数据
    $( ".search-btn").on("click",function () {
      var value=$(".search-txt").val()
      if(value.trim().length==0) {
          mui.toast("内容不能为空")
          return
      }
    var arr=JSON.parse(localStorage.getItem("search-list"))||[]
    var index=arr.indexOf(value)
      if(index!=-1) {
         arr.splice(index,1) 
      }
      arr.unshift(value)
      if(arr.length>6){
          arr.pop()
      }
      localStorage.setItem("search-list",JSON.stringify(arr))
      render()
      $('.search-txt').val('');
      location.href="./searchList.html?key="+value
    })
})
$(".lt-search-history").on("click",".search-next",function () {
    console.log( '111' );
    location.href="./searchList.html?key="+$(this).text()
})